package com.ruoyi.app.controller.sys.controller;

import com.ruoyi.app.common.annotation.PassToken;
import com.ruoyi.app.common.base.BaseAppController;
import com.ruoyi.app.common.exception.UnauthorizedException;
import com.ruoyi.app.common.page.ResultData;
import com.ruoyi.app.common.util.JwtUtil;
import com.ruoyi.app.controller.sys.entity.AppUser;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController extends BaseAppController
{
    @Autowired
    ISysUserService userService;

    @Autowired
    ISysDeptService sysDeptService;



    @PassToken
    @PostMapping("/login")
    public ResultData login(@RequestBody AppUser loginUser)
    {
        SysUser sysUser = userService.selectUserByLoginName(loginUser.getLoginname());
        // 这里是演示，正式使用要加密
        if (sysUser.getPassword().equals(encryptPassword(loginUser.getLoginname(),loginUser.getPassword(),sysUser.getSalt())))
        {
            return ResultData.success(JwtUtil.sign(sysUser.getLoginName(), sysUser.getPassword()));
        }
        else
        {
            throw new UnauthorizedException();
        }
    }

    @GetMapping("/getUserId")
    public ResultData getUserId()
    {
        return ResultData.success(getCurrentUserId());
    }

    @PassToken
    @PostMapping("register")
    public AjaxResult register(@RequestBody AppUser appUser){
        SysUser sysUser = new SysUser();
        sysUser.setSalt(randomSalt());
        sysUser.setLoginName(appUser.getLoginname());
        sysUser.setUserName(appUser.getUsername());
        sysUser.setPassword(encryptPassword(appUser.getUsername(),appUser.getPassword(),sysUser.getSalt()));
        sysUser.setRoleIds(new Long[]{2L});

        SysDept sysDept = sysDeptService.selectDeptById(111L);
        if( sysDept!= null){
            sysUser.setPostIds(new Long[]{111L});

            if (userService.selectUserByLoginName(appUser.getUsername()) == null){
                int num = userService.insertUser(sysUser);
                return toAjax(num);
            }
        }

        return AjaxResult.error("新建用户出错");
    }



    public String encryptPassword(String username, String password, String salt)
    {
        return new Md5Hash(username + password + salt).toHex().toString();
    }

    /**
     * 生成随机盐
     */
    public static String randomSalt()
    {
        // 一个Byte占两个字节，此处生成的3字节，字符串长度为6
        SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
        String hex = secureRandom.nextBytes(3).toHex();
        return hex;
    }
}